<?php defined('BASEPATH') OR exit('No direct script access allowed');

/**
 * Example
 *
 * This is an example of a few basic user interaction methods you could use
 * all done with a hardcoded array.
 *
 * @package		CodeIgniter
 * @subpackage	Rest Server
 * @category	Controller
 * @author		phuong101899
*/

// This can be removed if you use __autoload() in config.php OR use Modular Extensions
require APPPATH.'/libraries/REST_Controller.php';

class Control extends REST_Controller
{
    function __construct(){
        parent::__construct();
        $this->load->model('control/Control_model');
    }
    function order_post(){
        $user_id = $this->get_user()['id'];
        //valid
        if($this->post('table_id')){
            $table_id = $this->post('table_id');
        }else{
            $this->response(array('status'=>FALSE));
        }
        if($this->post('employee_id')){
            $employee_id = $this->post('employee_id');
        }else{
            $this->response(array('status'=>FALSE));
        }
        if($this->post('drink_id')){
            $drink_id = $this->post('drink_id');
        }else{
            $this->response(array('status'=>FALSE));
        }
        //prepare data
        if(preg_match('/,/', $drink_id)){
            $drink_id = explode(',', $drink_id);
        }else{
            $drink_id = array($drink_id);
        }
        if(is_array($drink_id)){
            foreach ($drink_id as $item) {
                list($drink_id,$count) = explode('_', $item);
                $data = array(
                    'drink_id'   => $drink_id,
                    'table_id'   => $table_id,
                    'employee_id'=> $employee_id,
                    'user_id'    => $user_id,
                    'quantum'    => $count
                );
                $check_duplication = $this->Control_model->check_duplication(array('drink_id'=>$drink_id,'table_id'=>$table_id,'user_id'=>$user_id));
                if(is_numeric($check_duplication) && $check_duplication>0){
                    $flag = $this->Control_model->update_duplication($count,$user_id,$table_id,$drink_id);
                }else{
                    $flag = $this->Control_model->add($data);
                }
                if(!$flag) $this->response(array('status'=>FALSE));
            }
            $this->response(array('status'=>TRUE));
        }
        $this->response(array('status'=>FALSE));

    }
    function bill_post(){
        $table_id = $this->post('table_id');
        if(!$table_id) $this->response(array('status'=>FALSE));
        $where = array(
            'table_id' => $table_id,
            'order.user_id'  => $this->get_user()['id']
        );
        $data = $this->Control_model->get_bill($where);
        if(is_array($data)){
            $this->response(array('status'=>TRUE,'data'=>$data));
        }
        $this->response(array('status'=>FALSE,'data'=>$data));
    }
    function table_get(){
        $user_id = $this->get_user()['id'];
        $data = $this->Control_model->table_list(array('table.user_id'=>$user_id));
        if(is_array($data)){
            $this->response(array('status'=>TRUE,'data'=>$data));
        }
        $this->response(array('status'=>FALSE,'data'=>$data));
    }
    private function get_user(){
        if(isset($_SESSION['user'])){
            return $_SESSION['user'];
        }  
    }
}